package 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.FocusEvent;
	import flash.events.Event;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextField;
	import flash.text.TextFormat;
	public class DateChooser extends Sprite
	{
		private static var _main:DateChooser;
		public static function get main():DateChooser
		{
			if(_main==null)
				_main = new DateChooser();
			return _main;
		}
		private static var bg:Sprite;
		public function DateChooser()
		{
			var stageW:int = test.stageW;
			var stageH:int = test.stageH;

			if(bg==null)
			{
				bg = new Sprite;
				var alpha_bg:Shape = null;
				if(alpha_bg == null){
					alpha_bg= new Shape();
					alpha_bg.graphics.beginFill(0x999999);
					alpha_bg.graphics.drawRect(0,0, stageW, stageH);
					alpha_bg.graphics.endFill();
					alpha_bg.alpha = .5;
				}
				bg.addEventListener(MouseEvent.CLICK,_hide);
				bg.addChild(alpha_bg);
				bg.y = - bg.height;
				addChildAt(bg,0);
			}

		}
		private static var datetype:String = "S";
		private static var choserBox:Sprite;
		private static var freeBtn:Sprite;
		private static var slideBtn:Sprite;
		private static var commitBtn:Sprite;
		private static var cancelBtn:Sprite;
		private static var gongliBtn:Sprite;
		private static var nongliBtn:Sprite;
		private static function newSprite(w:int,h:int,fun:Function=null):Sprite
		{
			var mc:Sprite = new Sprite();
			mc.addChild(new Bitmap(new BitmapData(2,2)));
			mc.width = w;
			mc.height = h;
			mc.alpha = 0;
			mc.mouseChildren = false;
			if(fun!=null)
				mc.addEventListener(MouseEvent.CLICK,fun);
			return mc;
		}
		//DateChooser.show(islunar,date,sprite,dateModified);
		private static var sprite:Sprite;
		private static var completeFunc:Function;
		public static function show(islunar:String="S",date:String=null,_sprite:Sprite=null,dateModified:Function=null):void
		{
			main.visible = true;
			sprite = _sprite;
			completeFunc = dateModified;
			test.mains.addChild(main);
			showFree(false);
		}
		private static function _showFree(e:Event=null):void
		{
			showFree(true);
		}
		private static function _showSlide(e:Event=null):void
		{
			showFree(false);
		}
		private static function _hide(e:Event=null):void{
			main.visible = false;
		}
		private static function _comit(e:Event=null):void{
			//main.visible = false;
			//logs.adds("gongli:",gongliBtn.alpha==1.0);
			//logs.adds("isFreeMode:",isFreeMode);
			var islunar:String;
			if(gongliBtn.alpha==1.0)
				islunar = "S";
			else
				islunar = "M";

			if(completeFunc!=null)
			{
				if(isFreeMode){
					//logs.adds("date:",freeTxt.text);
					completeFunc(sprite,islunar,freeTxt.text,"");
				}else{
					//logs.adds("date:",centureyRoll.value,yesrRoll.value,monthRoll.value,dayRoll.value);
					var year:String = ""+centureyRoll.value;
					if(yesrRoll.value.length<2)
						year = year + "0" + yesrRoll.value;
					else
						year = year + yesrRoll.value;
					var value:String = (year+"-"+(monthRoll.value)+"-"+(dayRoll.value));
					completeFunc(sprite,islunar,value,"");
				}
			}
			main.visible = false;
		}
		private static function changeGongNongli(e:Event):void
		{
			if(e.target==gongliBtn){
				gongliBtn.alpha = 1.0;
				nongliBtn.alpha = 0.0;
				datetype = "S";
			}else{
				gongliBtn.alpha = 0.0;
				nongliBtn.alpha = 1.0;
				datetype = "M";
			}
		}
		private static var isFreeMode:Boolean = false;
		private static var freeTxt:TextField ;
		private static function showFree(b:Boolean):void
		{
			isFreeMode = b;
			choserBox = Assets.selectFreeDateChoser(choserBox,b);
			main.addChild(choserBox);
			if(!b){
				if(freeBtn==null){
					freeBtn = newSprite(test.stageW/2,90,_showFree);
					freeBtn.x = test.stageW/2;
				}
				main.addChild(freeBtn);
			}else{
				if(slideBtn==null){
					slideBtn= newSprite(test.stageW/2,90,_showSlide);
				}
				main.addChild(slideBtn);
			}
			var mainHeight:int = 550;
			main.y = test.stageH - mainHeight;
			if(commitBtn==null){
				commitBtn = newSprite(test.stageW/2,90,_comit);
				commitBtn.x = test.stageW/2;
				commitBtn.y = mainHeight - commitBtn.height
			}
			if(cancelBtn==null){
				cancelBtn = newSprite(test.stageW/2,90,_hide);
				cancelBtn.y = mainHeight - cancelBtn.height
			}
			main.addChild(commitBtn);
			main.addChild(cancelBtn);
			if(gongliBtn==null){
				gongliBtn = Assets.newGongli();
				gongliBtn.x = 30;
				gongliBtn.addEventListener(MouseEvent.CLICK,changeGongNongli);
			}
			if(nongliBtn==null){
				nongliBtn= Assets.newNongli();
				nongliBtn.x = 200;
				nongliBtn.addEventListener(MouseEvent.CLICK,changeGongNongli);
			}
			gongliBtn.y = nongliBtn.y = 118;
			main.addChild(nongliBtn);
			main.addChild(gongliBtn);
			if(datetype=="S")
			{
				gongliBtn.alpha = 1.0;
				nongliBtn.alpha = 0.0;
			}else{
				gongliBtn.alpha = 0.0;
				nongliBtn.alpha = 1.0;
			}

			if(freeTxt==null)
			{
				//public static function makeTxtLine(str:String,size:int,color:uint = 0x000000):TextField
				freeTxt = new TextField();
				freeTxt.defaultTextFormat = new TextFormat("Microsoft YaHei", 30,0x000000);
				freeTxt.width = 600;
				freeTxt.height = 60;
				freeTxt.multiline = false;
				freeTxt.wordWrap = false;
				//freeTxt.autoSize = TextFieldAutoSize.NONE;
				freeTxt.type = "input";
				CONFIG::debugging{
					freeTxt.border = true;
				}
				freeTxt.background = true;
				freeTxt.x = 50;
				freeTxt.y = 290;
				freeTxt.addEventListener(Event.CHANGE,showTxt);
				freeTxt.addEventListener(FocusEvent.FOCUS_IN,showTxt);
				freeTxt.addEventListener(FocusEvent.FOCUS_OUT,showTxt);
			}
			freeTxt.visible = b;
			if(rollContainer)
				rollContainer.visible = !b;
			if(b){
				main.addChild(freeTxt);
				showTxt(null);
			}else{
				initRoll(true);
				if(rollContainer)
					main.addChild(rollContainer);
			}
		}
		private static function makeRoll(start:int,end:int,value:int,len:int=2):RollChoser
		{
			var rollchoser:RollChoser = (new RollChoser(start,end,len));
			var _value:String = String(value);
			while(_value.length<len)
				_value = "0"+_value;

			rollchoser.value = _value;
			return rollchoser;
		}
		private static var centureyRoll:RollChoser;
		private static var yesrRoll:RollChoser;
		private static var monthRoll:RollChoser;
		private static var dayRoll:RollChoser;
		private static var rollContainer:Sprite;
		private static function initRoll(_v:Boolean=true):Sprite
		{
			if(rollContainer ==null)
			{
				var _date:Date = new Date();
				logs.adds(_date,_date.fullYear,_date.getMonth(),_date.getDate());
				rollContainer = new Sprite;
				if(centureyRoll==null){
					centureyRoll = makeRoll(0,20,20,1);
					centureyRoll.x = 70;
					rollContainer.addChild(centureyRoll);
				}
				//return rollContainer;
				if(yesrRoll==null){
					yesrRoll = makeRoll(0,99,_date.fullYear%100);
					yesrRoll.x = 170;
					rollContainer.addChild(yesrRoll);
				}
				if(monthRoll==null){
					monthRoll = makeRoll(1,12,int(_date.getMonth())+1);
					monthRoll.x = 350;
					rollContainer.addChild(monthRoll);
				}
				if(dayRoll==null){
					dayRoll = makeRoll(1,31,int(_date.getDate()));
					dayRoll.x = 560;
					rollContainer.addChild(dayRoll);
				}
				centureyRoll.y =
					yesrRoll.y =
					monthRoll.y =
					dayRoll.y = 280;
			}
			rollContainer.visible = _v;
			return rollContainer;
		}
		private static function showTxt(e:Event):void
		{
			if(freeTxt.text.length>0)
				freeTxt.alpha = 1.0;
			else
				freeTxt.alpha = 0.0;
			if(e)
			{
				switch(e.type)
				{
					case FocusEvent.FOCUS_IN:
						freeTxt.alpha = 1.0;
						break;
					case FocusEvent.FOCUS_OUT:
						break;
				}
			}
		}
	}

}
